package org.noote.test_analysegrammaticale;

import java.util.ArrayList;

import org.noote.libs.assistant.analyse.AnalyseGrammaticale;
import org.noote.libs.assistant.analyse.Dictionnaire;
import org.noote.libs.assistant.analyse.Element;
import org.noote.libs.assistant.analyse.Grammaire;
import org.noote.libs.assistant.analyse.Phrase;
import org.noote.libs.assistant.analyse.Ponctuation;
import org.noote.libs.assistant.analyse.dictionnaire.Dictionnaire_FR_FR;
import org.noote.libs.assistant.analyse.grammaire.Grammaire_FR_FR;
import org.noote.libs.assistant.analyse.ponctuation.Ponctuation_FR_FR;
import org.noote.libs.assistant.agent.Agent_Messanger_FR;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestAnalyseGrammaticale_Activity extends Activity {

	protected AnalyseGrammaticale _ag = new AnalyseGrammaticale();
	protected Ponctuation _ponctFR = new Ponctuation_FR_FR();
	protected Dictionnaire _dicoFR = new Dictionnaire_FR_FR();
	protected Grammaire _gramFR = new Grammaire_FR_FR();
	protected Agent_Messanger_FR _agent = new Agent_Messanger_FR();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button buttonTextProcessing = (Button) findViewById(R.id.buttonTextProcessing);
		buttonTextProcessing.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText editTextToProcess = (EditText) findViewById(R.id.editTextToProcess);
				String sTextToProcess = editTextToProcess.getText().toString();

				// process text :)
				process(sTextToProcess);
			}
		});

		EditText editTextToProcess = (EditText) findViewById(R.id.editTextToProcess);
		editTextToProcess.setText("Ceci est un test.");
	}


	private void clearLog() {
		TextView textViewLog = (TextView) findViewById(R.id.textViewLog);
		textViewLog.setText("");
	}
	void addLog(String log) {
		TextView textViewLog = (TextView) findViewById(R.id.textViewLog);
		if(textViewLog.getText().toString().isEmpty())
			textViewLog.setText(log);
		else
			textViewLog.setText(textViewLog.getText() + "\n" + log);
	}

	protected void process(String sTextToProcess) {
		EditText editTextToProcess = (EditText) findViewById(R.id.editTextToProcess);
		editTextToProcess.setEnabled(false);
		
		clearLog();
		addLog("Analyse FR : '"+sTextToProcess+"'...");
		if (_ag.analyse(_ponctFR, _dicoFR, _gramFR, sTextToProcess)) {
			
			ArrayList<Phrase> phrases = _ag.getPhrases();
			if(phrases.size()>0)
			{
				addLog("Phrase count : "+phrases.size());
				for(Phrase p : phrases)
				{
					 String elFormat = "";
					 for(Element el : p._elements)
					 {
						 elFormat += el.toString() + " ";
					 }
					 
					 addLog(elFormat);
				}
				
				// use _agent :)
				for(Phrase p : phrases)
				{
					 String elReponse = _agent.repondre(p);
					 
					 if(!elReponse.isEmpty())
						 addLog(elReponse);
				}
			}
			else
			{
				addLog("No phrase parsed");
				
				 ArrayList<Element> elems = _ag.getElements();
				 
				 String elFormat = "";
				 for(Element el : elems)
				 {
					 elFormat += el.toString() + " ";
				 }
				 
				 addLog(elFormat);
			}
		}
		else addLog("Failed :(");
		editTextToProcess.setEnabled(true);
	}
}
